package com.example.weatherapp1.data.local

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.weatherapp1.data.model.City
import kotlinx.coroutines.flow.Flow

@Dao
interface CityDao {
    @Insert
    suspend fun insertCity(city: City)

    @Update
    suspend fun updateCity(city: City)

    @Query("DELETE FROM cities WHERE id = :cityId")
    suspend fun deleteCity(cityId: Int)

    @Query("SELECT * FROM cities ORDER BY isCurrentLocation DESC, name ASC")
    fun getAllCities(): Flow<List<City>>

    @Query("SELECT * FROM cities WHERE id = :cityId")
    suspend fun getCityById(cityId: Int): City?

    @Query("SELECT * FROM cities WHERE isCurrentLocation = 1 LIMIT 1")
    suspend fun getCurrentLocationCity(): City?

    @Query("UPDATE cities SET isCurrentLocation = 0 WHERE isCurrentLocation = 1")
    suspend fun resetCurrentLocation()
}
